<?php
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Method:POST,GET');

use yii\helpers\Url;
$this->title = '中国人事网';

?>
<div class="appr_sidebar m_left_menu">
    <?=$this->render("@app/views/public/_staffLeftMenu") ?>
</div>
<!--<a class="back-to-top" href="#top">-->
<!--    返回顶部-->
<!--</a>-->
<div class="appr_bd_wrap">
    <div class="judge_wrap">
        <div class="judge_wrap judge_size_b evaluate_detail">
            <div class="appr_bread">
                <em>员工信息(HR)</em>
            </div>
            <div class="judge_content">
                <div id="grid-list"></div>
            </div>
        </div>
    </div>
</div>
<script type="text/x-kendo-template" id="toolbarTemplate">
    <a class="k-button k-button-icontext m-grid-list-btn k-state-disabled" onclick="refreshList(this,'wxf')"><i class="glyphicon glyphicon-th-list"></i>万先丰贸易</a>
    <a class="k-button k-button-icontext m-grid-list-btn" onclick="refreshList(this,'hd')"><i class="glyphicon glyphicon-th-list"></i>华定科技</a>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <a class="k-button k-button-icontext" onclick="onAdd();"><span class="k-icon k-add"></span>新增</a>
    <a class="k-button k-button-icontext" onclick="onExport();"><span class="k-icon k-i-excel"></span>条件导出</a>
    <a class="k-button k-button-icontext k-grid-excel"><span class="k-icon k-i-excel"></span>导出页面数据</a>
</script>





<script>

var _grid = $("#grid-list"); //grid
var dataSource;//grid 数据
var exportWindow = $("<div id='grid-export'></div>");//导出
var addWindow = $("<div id='popupWindow'></div>");//添加
var editWindow =null;
var alignCenterAttribute = { style: "text-align: center" };//剧中
var detailWindow = $("<div id='grid-detail'></div>");//添加
var _company = "wxf";


$(document).ready(function () {
    dataSource = new kendo.data.DataSource({
        transport: {
            read: function (e) {
                $.ajax({

                    url: "<?=Url::to(['list']); ?>",
                    type:"GET",
                    data:{company:_company},
                    dataType: "json",
                    success:function (redata) {
                        e.success(redata);
                    }

                });
            },
            parameterMap: function(options, operation) {
                if (operation !== "read" && options.models) {
                    return {models: kendo.stringify(options.models)};
                }
            }
        },
        batch: true,
        pageSize: 20,

        schema: {
            model: {
                id: "id",
                fields: {
                    id: { field:"id" ,editable: false, nullable: true },
                    name_cn_long: { field:"name_cn_long",type: "string"},
                    name_en_long: { field:"name_en_long",type: "string"},
                    department_name: { field:"department_name" ,type: "string"},
                    position_name: { field:"position_name", type: "string" },
                    group_name :{field:"group_name" ,type:"string"},
                    hired_date :{field:"hired_date" ,type: "string"},
                    terminated_date :{field:"terminated_date" ,type: "string"},
                    position_status : { field:"position_status" ,type:"string" },
                    finger_print_number :{ field:"finger_print_number" ,type:"string"}
                }
            },
            data:function(d){
                return d.list;

            },
            total:function(d){
        
                return d.count
            }
        },
        filter: {
            logic: "or",
            filters:[
                {field:"position_status", operator:"eq", value:"实习期"},
                {field:"position_status", operator:"eq", value:"在职"}
            ]
        },

    });





    var _departmentValue = new Array;


    _grid.kendoGrid({
        dataSource: dataSource,
        sortable: true,
        reorderable: true,
        groupable: true,
        resizable: true,
        //filterable: { mode: "menu, row" },
        filterable: {
            extra: false,
            operators: {
                string: {
                    contains: "包含",
                    eq: "等于",
                    neq: "不等于",
                    doesnotcontain: "不包含",
                    isnull:"Is Null",
                    isnotnull:"Is Not Null",
                    isempty:"Is Empty",
                    isnotempty:"Is Not Empty"
                }
            }
        },
        columnMenu: true,
        height:870,
        toolbar:kendo.template($("#toolbarTemplate").html()),
        pageable:{
            refresh: true,
            pageSizes: true,
            buttonCount: 10
        },
        excel: {
            allPages: true,
            fileName:"员工信息表.xlsx",
            filterable: true
        },
        editable: "popup",
        columns: [
            { 
                field:"name_cn_long", 
                title: "中文名" ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },
            { 
                field: "name_en_long", 
                title:"英文名" ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },
            { 
                field: "department_name", 
                title:"部门",
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute,
                filterable: {
                    multi: true,
                    search: true,
                    checkAll: false
                }
            },
            { 
                field: "group_name", 
                title:"组别",
                hidden:true ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute,
                filterable: {
                    multi: true,
                    search: true
                }
            },
            { 
                field: "position_name", 
                title:"职位" ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute,
                filterable: {
                    multi: true,
                    search: true
                }
            },
            { 
                field: "finger_print_number",
                title:"指纹号" ,
                hidden:true,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },

            { 
                field: "hired_date",
                title:"入职日期" ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },

            {
                field: "terminated_date",
                title:"离职日期" ,
                hidden:true ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            },

            { 
                field: "position_status",
                title:"岗位状态" ,
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute,
                filterable: {
                    multi: true,
                    search: true
                }
            },

            { 
                command: [
                    {
                        text:"编辑",
                        imageClass:"k-icon k-edit",
                        click:onEdit
                    },
                    {
                        text:"Detail",
                        imageClass:"",
                        click:onDetail
                    }
                ], 
                title: "", 
                width: "180px",
                headerAttributes: alignCenterAttribute,
                attributes: alignCenterAttribute
            }
        ],
        /*
        dataBinding:function (e) {
            var _isSelectDepartment = false;
            var _selectDepartmentValue = new Array;
            var filters = e.sender.options.dataSource.options.filter.filters;
            for (var i =0;i < filters.length;i++){
                if( filters[i].field == 'department_name'){
                    _isSelectDepartment = true;

                    _selectDepartmentValue.push(filters[i].value)
                }
                if(!$.isEmptyObject(filters[i].filters)){
                    for (var k=0;k < filters[i].filters.length;k++){
                        if( filters[i].filters[k].field == 'department_name'){
                            //判断是否选中部门
                            _isSelectDepartment = true;
                            _selectDepartmentValue.push(filters[i].filters[k].value);
                        }
                    }
                }
            }

            //判断是否选中部门
            if(_isSelectDepartment){

                for (var i = 0;i < e.sender.options.columns.length ; i++){

                    if(e.sender.options.columns[i].field == 'group_name' && _departmentValue.toString() != _selectDepartmentValue.toString()){

                        _departmentValue = _selectDepartmentValue;

                        var grid = _grid.data("kendoGrid");

                        var _data = new Array;

                        for (var k=0;k < grid.dataSource._data.length;k++){
                            if(_selectDepartmentValue.indexOf(grid.dataSource._data[k].department_name) >= 0){
                                if(_data.indexOf(grid.dataSource._data[k].group_name) < 0){
                                    _data.push(grid.dataSource._data[k].group_name);
                                }
                            }
                        }

                        var _filterData = new Array;

                        for (var t=0;t < _data.length;t++){
                            if(_data[t] != ''){
                                _filterData.push({
                                    text:_data[t]
                                })
                            }

                        }



                        grid.setOptions({
                            columns: [
                                {
                                    field:"name_cn_long",
                                    title: "中文名" ,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute
                                },
                                {
                                    field: "name_en_long",
                                    title:"英文名" ,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute
                                },
                                {
                                    field: "department_name",
                                    title:"部门",
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute,
                                    filterable: {
                                        multi: true,
                                        search: true,
                                        checkAll: false
                                    }
                                },
                                {
                                    field: "group_name",
                                    title:"组别",
                                    hidden:true ,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute,
                                    filterable: {
                                        multi: true,
                                        search: true,
                                        cell:{
                                            dataSource: new kendo.data.DataSource({
                                                data: _filterData
                                            }),
                                            dataTextField : "text"
                                        }
                                    }
                                },
                                {
                                    field: "position_name",
                                    title:"职位" ,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute,
                                    filterable: {
                                        multi: true,
                                        search: true
                                    }
                                },
                                {
                                    field: "finger_print_number",
                                    title:"指纹号" ,
                                    hidden:true,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute
                                },

                                {
                                    field: "hired_date",
                                    title:"入职日期" ,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute
                                },

                                {
                                    field: "position_status",
                                    title:"岗位状态" ,
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute,
                                    filterable: {
                                        multi: true,
                                        search: true
                                    }
                                },

                                {
                                    command: [
                                        {
                                            text:"编辑",
                                            imageClass:"k-icon k-edit",
                                            click:onEdit
                                        },
                                        {
                                            text:"Detail",
                                            imageClass:"",
                                            click:onDetail
                                        }
                                    ],
                                    title: "",
                                    width: "180px",
                                    headerAttributes: alignCenterAttribute,
                                    attributes: alignCenterAttribute
                                }
                            ]
                        });


                        grid.refresh();
                    }
                }



            }


        }
        */
    });

});

// function onEdit(e){
//     var tr = $(e.target).closest("tr");
//     var data = this.dataItem(tr);
//     if (!editWindow.data("kendoWindow")) {
//         editWindow.kendoWindow({
//             width: "800px",
//             height:"800px",
//             title: "编辑",
//             actions: [
//                 "Close"
//             ],
//             position:{
//                 left:"30%",
//                 top:"20%"
//             }

//         }).data("kendoWindow");
//     }
//     editWindow.data("kendoWindow").refresh({
//         url: "<?=Url::to(['edit']) ?>",
//         data: { id: data.id }
//     });

//     editWindow.data("kendoWindow").open();
// }

function refreshList(_this,company) {
    $(".m-grid-list-btn").each(function (index,el) {
        $(el).kendoButton().data("kendoButton").enable(true)
    })

    $(_this).kendoButton({
        enable: false
    });
    _company = company;
    var grid = $("#grid-list").data("kendoGrid");
    grid.dataSource.read();

}

function onDetail(e) {
    var tr = $(e.target).closest("tr");
    var data = this.dataItem(tr);

    if (!detailWindow.data("kendoWindow")) {
        detailWindow.kendoWindow({
            width: "1280px",
            height:"900px",
            title: "编辑",
            minWidth: 1024,
            minHeight: 890,
            modal: true,

            actions: [
                "pin",
                "minimize",
                "maximize",
                "close"
            ]
        }).data("kendoWindow");
    }

    detailWindow.data("kendoWindow").refresh({
        url: "<?=Url::to(['detail']) ?>",
        data:{id:data.id}
    });
    detailWindow.data("kendoWindow").center().open();
}

function test() {
    alert(2)
}

function onEdit(e){
    var tr = $(e.target).closest("tr");
    var data = this.dataItem(tr);
    var id = data.id.toLowerCase();
    editWindow = $("<div id='popupWindow"+id+"'></div>");
    if (!editWindow.data("kendoWindow")) {
        editWindow.kendoWindow({
            width: "1280px",
            height:"900px",
            title: "编辑",
            minWidth: 1024,
            minHeight: 890,
            modal: true,
            close: function (e) {
                this.destroy();
            },
            actions: [
                "pin",
                "minimize",
                "maximize",
                "close"
            ]
        }).data("kendoWindow");
    }
    // editWindow.data("kendoWindow").refresh({
    //     url: "http://foundation.sjfood.us/Staff/Update?staffId="+data.id
    // });
    
    editWindow.data("kendoWindow").refresh({
        url: "<?=Url::to(['edit']) ?>",
        data:{id:data.id}
    });
    
    editWindow.data("kendoWindow").center().open();

}



function onExport(){
    if (!exportWindow.data("kendoWindow")) {
        exportWindow.kendoWindow({
            width: "600px",
            title: "编辑",
            actions: [
                "Close"
            ],
            close: function (e) {
                this.destroy();
            },
            position:{
                left:"30%",
                top:"20%"
            }

        }).data("kendoWindow");
    }
    exportWindow.data("kendoWindow").refresh({
        url: "<?=Url::to(['exportview']) ?>"
    });

    exportWindow.data("kendoWindow").open();
}


function onAdd(){
    if (!addWindow.data("kendoWindow")) {
        addWindow.kendoWindow({
            width: "1280px",
            height:"900px",
            title: "编辑",
            minWidth: 1024,
            minHeight: 890,
            modal: true,
            actions: [
                "pin",
                "minimize",
                "maximize",
                "close"
            ],
            close: function (e) {
                this.destroy();
            }
        }).data("kendoWindow");
    }

    // addWindow.data("kendoWindow").refresh({
    //     url: "<?=Url::to(['add']) ?>"
    // });
    
    addWindow.data("kendoWindow").refresh({
        url: "<?=\YII::$app->params["foudationUrl"] ?>/Staff/Create"
    });
    addWindow.data("kendoWindow").center().open();
}
</script>

